package com.shwread.android.qysw.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shwread.android.qysw10000182.R;
import com.shwread.android.ui.customview.MyListView;
import com.shwread.android.ui.widget.CircleImageView;
import com.shwread.android.ui.widget.CommentView;
import com.shwread.android.ui.widget.LoadingView;
import com.shwread.android.ui.widget.RegionContentView;
import com.shwread.android.ui.widget.xlistview.XListView;
import com.shwread.android.ui.widget.xlistview.XListView.IXListViewListener;
import com.shwread.httpsdk.http.callback.BaseAction;
import com.shwread.httpsdk.http.face.DelCommentAction;
import com.shwread.httpsdk.http.face.QryCommentListByUserAction;
import com.shwread.httpsdk.http.face.QryCommentMoreByUserAction;

import java.util.List;

import logic.bean.BookInfo;
import logic.bean.CommentBean;
import logic.bean.UserCommentBean;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.util.DateUtil;
import logic.util.ImageLoaderUtil;
import logic.util.PicassoUtil;
import logic.util.ScreenUtil;
import logic.util.Util;


/**
 * @Description：我的评论
 * @Author：zouqp
 * @Date：2015-02-03
 */

public class MyCommentActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener,
        IXListViewListener
{
	public static final int       PAGESIZE               = 10;
	public static final int       INIT_COMMENTITEMS_SIZE = 4;
	private static final char     COMMENTS_SPLITOR       = 8;
	private TextView              tvTitle;
	private Button                btnBack;
	private XListView             lv_mycomment;
	private CommentView           addCommentView;
	private LinearLayout          ll_input;
	private List<UserCommentBean> myComments;
	private Context               mContext;
	private MycommentAdapter      myAdapter;
	private boolean               hasMore                = true;
	private int                   currentPage            = 1;
	private long                  currentPublishTime     = 0;
	private boolean               isInitLoad             = true;
	private LoadingView           loadingView;
	
	private Handler               handler                = new Handler()
	                                                     {
		                                                     @Override
		                                                     public void handleMessage(Message msg) {
			                                                     switch (msg.what) {
																	 case DefaultConsts.COMMENT_SUBMIT_SUCCESS:
																		 getMycomments();//刷新评论
																		 addCommentView.setVisibility(View.GONE);
																		 break;
																	 case DefaultConsts.COMMENT_SUBMIT_ERROR:
																		 break;
																 }
															 }
	                                                     };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		initViews();
		initData();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		closeLoading();
	}
	
	@Override
	public Handler getUIHandler() {
		return handler;
	}
	
	private void initViews() {
		setContentView(R.layout.comment_mycomment_layout);
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText("我的评论");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		btnBack.setOnClickListener(this);
		
		FrameLayout fl_mycomment = (FrameLayout) findViewById(R.id.fl_mycomment);
		
		//添加增加评论的输入框
		ll_input = (LinearLayout) findViewById(R.id.ll_input);
		addCommentView = new CommentView(this);
		ll_input.addView(addCommentView);
		addCommentView.setVisibility(View.GONE);
		//初始化外面的这个list view
		lv_mycomment = (XListView) findViewById(R.id.lv_mycomment);
		myAdapter = new MycommentAdapter();
		lv_mycomment.setAdapter(myAdapter);
		lv_mycomment.setPullRefreshEnable(true);
		lv_mycomment.setPullLoadEnable(true);
		lv_mycomment.setXListViewListener(this);
		lv_mycomment.hideLoadMore();
		
		//添加出错和空的视图
		loadingView = new LoadingView(this);
		fl_mycomment.addView(loadingView);
		initLoadingView();
	}
	
	private void initLoadingView() {
		loadingView.setLoadingAnim(R.drawable.home_progress);
		//		loadingView.setLoadingAnim(R.anim.column_switch_anim);
		loadingView.setEmptyView(R.drawable.icon_comment_empty, R.string.my_comment_empty_text);
		loadingView.setFailView(R.drawable.icon_loading_failed, R.string.comment_loading_failed);
		loadingView.setFailClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) {
				initData();
			}
		});
	}
	
	/**
	 * @param flag
	 *            -1：加载失败 ， 0:加载中，1：加载为空，2为加载成功
	 */
	private void updateHintUI(int flag) {
		if (flag == 2)
		{
			lv_mycomment.setVisibility(View.VISIBLE);
			loadingView.setVisibility(View.GONE);
		}
		else
		{
			lv_mycomment.setVisibility(View.GONE);
			loadingView.setVisibility(View.VISIBLE);
		}
		loadingView.changeLoadingStatus(flag);
	}
	
	private void initData() {
		//启动动画
		isInitLoad = true;
		updateHintUI(0);
		getMycomments();
	}
	
	@Override
	public void onRefresh() {
		lv_mycomment.setReadyHint("");
		getMycomments();
	}
	
	@Override
	public void onLoadMore() {
		if (hasMore)
		{
			lv_mycomment.setReadyHint("");
			getMoreMycomments();
		}
		else
		{
			lv_mycomment.setReadyHint("没有更多了");
			lv_mycomment.hideLoadMore();
		}
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				addCommentView.hideInputMethod();
				onBackPressed();
				break;
			default:
				break;
		}
	}
	
	private class MycommentAdapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			if (myComments == null) return 0;
			return myComments.size();
		}
		
		@Override
		public Object getItem(int position) {
			return myComments.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View view, ViewGroup parent) {
			view = LayoutInflater.from(MyCommentActivity.this).inflate(R.layout.comment_book_item, null);
			MyCommentViewHolder commentViewHolder = new MyCommentViewHolder();
			commentViewHolder.ll_object_frame = view.findViewById(R.id.ll_object_frame);
			commentViewHolder.im_cover = (ImageView) view.findViewById(R.id.im_cover);
			commentViewHolder.tv_title = (TextView) view.findViewById(R.id.tv_title);
			commentViewHolder.tv_description = (TextView) view.findViewById(R.id.tv_description);
			commentViewHolder.ll_getmore_frame = view.findViewById(R.id.ll_getmore_frame);
			commentViewHolder.tv_get_more = (TextView) view.findViewById(R.id.tv_get_more);
			commentViewHolder.im_get_more = (ImageView) view.findViewById(R.id.im_get_more);
			commentViewHolder.im_getmore_anim = (ImageView) view.findViewById(R.id.im_getmore_anim);
			commentViewHolder.lv_commentitem = (MyListView) view.findViewById(R.id.lv_commentitem);
			
			UserCommentBean commentBean = myComments.get(position);
			if (commentBean.getObjType() != 1)
			{
				//设置非书籍信息的图片大小
				ViewGroup.LayoutParams layoutparams = commentViewHolder.im_cover.getLayoutParams();
				int width = ScreenUtil.dip2px(57);
				layoutparams.width = width;
				layoutparams.height = width;
				commentViewHolder.im_cover.setLayoutParams(layoutparams);
			}
			//设置数据
			commentViewHolder.ll_object_frame.setTag(position);
			commentViewHolder.ll_object_frame.setOnClickListener(objectOnclickListener);
			List<CommentBean> items = commentBean.getCommentList();
			
			commentViewHolder.ll_getmore_frame.setTag(position);
			commentViewHolder.ll_getmore_frame.setOnClickListener(getmoreOnclickListener);
			int status = 1;
			if (commentBean.getHasMoreComments())
			{
				status = 1; //加载更多
			}
			else
			{
				status = 3; //没有更多
			}
			MoreViewHolder moreViewHolder = commentBean.getMoreView();
			if (moreViewHolder == null)
			{
				moreViewHolder = new MoreViewHolder();
				moreViewHolder.status = status;
				commentBean.setMoreView(moreViewHolder);
			}
			moreViewHolder.ll_getmore_frame = commentViewHolder.ll_getmore_frame;
			moreViewHolder.tv_get_more = commentViewHolder.tv_get_more;
			moreViewHolder.im_get_more = commentViewHolder.im_get_more;
			moreViewHolder.im_getmore_anim = commentViewHolder.im_getmore_anim;
			commentBean.setMoreView(moreViewHolder);
			setLoadmoreViewContent(position, moreViewHolder.status);
			
			if (commentBean.getObjType() == 1)
			{
				//书籍
//				commentViewHolder.im_cover.setImageUrl(commentBean.getCoverImage());
				PicassoUtil.load(mContext, commentBean.getCoverImage(), R.drawable.bookcover_default, commentViewHolder.im_cover);
				commentViewHolder.tv_description.setText("作者: " + commentBean.getAuthor());
			}
			else
			{
				//其它的类型
//				commentViewHolder.im_cover.setImageUrl(commentBean.getCoverImage(), R.drawable.bg_small_image);
				PicassoUtil.load(mContext, commentBean.getCoverImage(), R.drawable.bg_small_image, commentViewHolder.im_cover);
				commentViewHolder.tv_description.setText(commentBean.getShortDesc());
			}
			commentViewHolder.tv_title.setText(commentBean.getTitle());
			
			//设置子list view 的属性
			CommentItemsAdapter itemsAdapter = commentBean.getItemsAdapter();
			if (itemsAdapter == null)
			{
				itemsAdapter = new CommentItemsAdapter(MyCommentActivity.this);
				itemsAdapter.setParentPosition(position);
				itemsAdapter.setCommentItemsList(commentBean.getCommentList());
				commentBean.setItemsAdapter(itemsAdapter);
			}
			commentViewHolder.lv_commentitem.setAdapter(itemsAdapter);
			commentViewHolder.lv_commentitem.setOnItemClickListener(commentItemClickListener);
			commentViewHolder.lv_commentitem.setOnItemLongClickListener(commentItemLongClickListener);
			return view;
		}
	}
	
	OnClickListener         objectOnclickListener        = new OnClickListener()
	                                                     {
		                                                     @Override
		                                                     public void onClick(View v) {
			                                                     Object objPosition = v.getTag();
			                                                     if (objPosition == null) return;
			                                                     int position = (Integer) objPosition;
			                                                     UserCommentBean commentBean = myComments.get(position);
			                                                     int contentType = commentBean.getObjType();
			                                                     if (contentType == 1)
			                                                     { //单本书籍
				                                                   //跳转到书籍详情
				                                                     BookInfo bookInfo = new BookInfo();
				                                                     bookInfo.setBookId(commentBean.getObjId());
				                                                     bookInfo.setAuthor("");
				                                                     //#ifdef SHOW_OLDBOOKDETAIL
				                                                     //$ Intent intent = new Intent(mContext, BookDetailActivity.class);
				                                                     //#else
				                                                     Intent intent = new Intent(mContext, NewBookDetailActivity.class);
				                                                     //#endif
				                                                     intent.putExtra("BookInfo", bookInfo);
				                                                     mContext.startActivity(intent);
			                                                     }
			                                                     else if (contentType == 2)
			                                                     {//资讯
				                                                  // 跳转到图文详情
				                                                     gotoImageTextActivity(commentBean/*
																									   * .
																									   * getHtmlUrl
																									   * (
																									   * )
																									   * ,
																									   * commentBean
																									   * .
																									   * getTitle
																									   * (
																									   * )
																									   */);
			                                                     }
			                                                     else if (contentType == 3)
			                                                     {//图集的图片   直接看图片
				                                                  //gotoImageViewerActivity(position, 1);/查看单张图片
			                                                     }
			                                                     else if (contentType == 4)
			                                                     { //图集  跳转到图集
				                                                     gotoImageViewerActivity(position, 2);
			                                                     }
		                                                     }
	                                                     };
	
	OnClickListener         getmoreOnclickListener       = new OnClickListener()
	                                                     {
		                                                     @Override
		                                                     public void onClick(View v) {
			                                                     Object tagObject = v.getTag();
			                                                     if (tagObject == null) return;
			                                                     int parentPosition = (Integer) tagObject;
			                                                     UserCommentBean commentBean = myComments.get(parentPosition);
			                                                     int status = commentBean.getMoreView().status;
			                                                     int contentType = commentBean.getObjType();
			                                                     long contentId = commentBean.getObjId();
			                                                     //todo 调用接口，获取真实的数据   或者收起
			                                                     if (status == 1 || status == 5)
			                                                     {//原来为加载更多 或加载失败
				                                                  //执行加载更多
				                                                     getMoreCommentItems("" + contentType, "" + contentId, parentPosition);
			                                                     }
			                                                     else if (status == 2)
			                                                     {//原来为收回
				                                                  //执行收回
				                                                     List<CommentBean> items = commentBean.getCommentList();
				                                                     if (items == null || items.size() == 0) return;
				                                                     int count = items.size() < INIT_COMMENTITEMS_SIZE ? items.size() : INIT_COMMENTITEMS_SIZE - 1;
				                                                     List<CommentBean> newItems = items.subList(0, count);
				                                                     CommentItemsAdapter itemsAdapter = commentBean.getItemsAdapter();
				                                                     itemsAdapter.setCommentItemsList(newItems);
				                                                     status = 1;
				                                                     updateLoadmoreViewStatus(parentPosition, status);
				                                                     myAdapter.notifyDataSetChanged();
				                                                     //更新load view 的状态
			                                                     }
		                                                     }
	                                                     };
	
	//评论项的click 事件
	OnItemClickListener     commentItemClickListener     = new OnItemClickListener()
	                                                     {
		                                                     @Override
		                                                     public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
			                                                     if (addCommentView.getVisibility() == View.VISIBLE)
			                                                     {
				                                                     addCommentView.setVisibility(View.GONE);
				                                                     //隐藏输入法
				                                                     addCommentView.hideInputMethod();
				                                                     return;
			                                                     }
			                                                     Object tagOjb = view.getTag();
			                                                     if (tagOjb == null) return;
			                                                     int parentPosition = (Integer) tagOjb;
			                                                     UserCommentBean comment = myComments.get(parentPosition);
			                                                     List<CommentBean> items = comment.getCommentList();
			                                                     if (items == null || items.size() == 0) return;
			                                                     CommentBean itemBean = items.get(position);
			                                                     String commentUser = itemBean.getCommentUserAccount();
			                                                     
			                                                     String targetType = comment.getObjType() + "";
			                                                     String targetValue = comment.getObjId() + "";
			                                                     long commentId = itemBean.getCommentId();
			                                                     
			                                                     if (Const.phone_number.equalsIgnoreCase(commentUser))
			                                                     {
				                                                     //点击到了自己的评论
				                                                     addCommentView.setDefaultHint();
				                                                     addCommentView.setCommentParams(MyCommentActivity.this, targetType, targetValue);
			                                                     }
			                                                     else
			                                                     {
				                                                     //点击到了别人回复的评论
				                                                     long targetUserId = itemBean.getCommentUserId();
				                                                     addCommentView.setHintString(itemBean.getCommentUserNickName());
				                                                     addCommentView.setReplyParams(MyCommentActivity.this, targetType, targetValue, targetUserId, commentUser, commentId);
			                                                     }
			                                                     addCommentView.setVisibility(View.VISIBLE);
			                                                     //显示输入法
			                                                     addCommentView.showInputMethod();
			                                                     //Util.showInputMethodManager(mContext);
		                                                     }
	                                                     };
	
	//评论项的长按 事件
	OnItemLongClickListener commentItemLongClickListener = new OnItemLongClickListener()
	                                                     {
		                                                     @Override
		                                                     public boolean onItemLongClick(AdapterView adapterview, View view, int position, long arg3) {
			                                                     Object tagOjb = view.getTag();
			                                                     if (tagOjb == null) return false;
			                                                     int parentPos = (Integer) tagOjb;
			                                                     showDeleteCommentItemDialog(parentPos, position);
			                                                     return true;
		                                                     }
	                                                     };
	
	private void showDeleteCommentItemDialog(final int parentPosition, int position) {
		UserCommentBean comment = myComments.get(parentPosition);
		List<CommentBean> items = comment.getCommentList();
		if (items == null || items.size() == 0) return;
		CommentBean itemBean = items.get(position);
		String commentUser = itemBean.getCommentUserAccount();
		if (!Const.phone_number.equalsIgnoreCase(commentUser)) { return;//评论帐户与当前帐户不相同， 不能删除此条评论
		}
		final String targetType = comment.getObjType() + "";
		final String targetValue = comment.getObjId() + "";
		final long commentId = itemBean.getCommentId();
		final DeleteCommentItemDialog deleteDialog = new DeleteCommentItemDialog(this);
		deleteDialog.setOkOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v) {
				deleteDialog.dismiss();
				//调用删除评论的方法
				deleteCommentItem(parentPosition, targetType, targetValue, commentId);
			}
		});
		deleteDialog.show();
	}
	
	/**
	 *            当前的loadmore view
	 * @param status
	 *            ;//1为加载更多， 2 为收回， 3为隐藏， 4为正在加载， 5 为加载失败
	 */
	private void setLoadmoreViewContent(int parentPosition, int status) {
		UserCommentBean commentBean = myComments.get(parentPosition);
		MoreViewHolder moreViewHolder = commentBean.getMoreView();
		moreViewHolder.status = status;
		if (status == 3)
		{
			moreViewHolder.ll_getmore_frame.setVisibility(View.GONE);
		}
		else
		{
			moreViewHolder.ll_getmore_frame.setVisibility(View.VISIBLE);
			moreViewHolder.ll_getmore_frame.setClickable(true);
			TextView tv_loadmore = moreViewHolder.tv_get_more;
			tv_loadmore.setTextColor(0xfff79535);
			ImageView im_loadmore = moreViewHolder.im_get_more;
			im_loadmore.setVisibility(View.VISIBLE);
			ImageView im_loadmore_anim = moreViewHolder.im_getmore_anim;
			im_loadmore_anim.setVisibility(View.GONE);
			if (status == 2)
			{
				tv_loadmore.setText("收回");
				im_loadmore.setImageResource(R.drawable.icon_comment_return);
			}
			else if (status == 1)
			{
				tv_loadmore.setText("加载更多");
				im_loadmore.setImageResource(R.drawable.icon_comment_expand);
			}
			else if (status == 4)
			{
				tv_loadmore.setText("正在加载...");
				im_loadmore.setVisibility(View.GONE);
				im_loadmore_anim.setVisibility(View.VISIBLE);
				AnimationDrawable loadingAnim = (AnimationDrawable) im_loadmore_anim.getDrawable();
				moreViewHolder.ll_getmore_frame.setClickable(false);
				loadingAnim.start();
			}
			else if (status == 5)
			{
				tv_loadmore.setText("加载失败，请稍后再试");
				tv_loadmore.setTextColor(0xff676767);
				im_loadmore.setVisibility(View.GONE);
			}
		}
	}
	
	/**
	 *            当前的loadmore view
	 * @param status
	 *            ;//1为加载更多， 2 为收回， 3为隐藏， 4为正在加载， 5 为加载失败
	 */
	private void updateLoadmoreViewStatus(int parentPosition, int status) {
		UserCommentBean commentBean = myComments.get(parentPosition);
		MoreViewHolder moreViewHolder = commentBean.getMoreView();
		moreViewHolder.status = status;
		myAdapter.notifyDataSetChanged();
		return;
		
		//		int visiblePosition = lv_mycomment.getFirstVisiblePosition();  
		//		if(parentPosition - visiblePosition < 0 ){
		//			myAdapter.notifyDataSetChanged();
		//			return ;
		//		}
		//		View itemView = lv_mycomment.getChildAt(parentPosition - visiblePosition);  
		//		View ll_getmore_frame = itemView.findViewById(R.id.ll_getmore_frame); 
		//		if(status == 3 ) {
		//			ll_getmore_frame.setVisibility(View.GONE);
		//		} else {
		//			ll_getmore_frame.setVisibility(View.VISIBLE);
		//			ll_getmore_frame.setClickable(true);
		//			TextView tv_loadmore = (TextView)itemView.findViewById(R.id.tv_get_more);
		//			tv_loadmore.setTextColor(0xfff79535);
		//			ImageView im_loadmore = (ImageView)itemView.findViewById(R.id.im_get_more);
		//			im_loadmore.setVisibility(View.VISIBLE);
		//			ImageView im_loadmore_anim = (ImageView)itemView.findViewById(R.id.im_getmore_anim);
		//			im_loadmore_anim.setVisibility(View.GONE);
		//			if ( status == 2 ) {
		//				tv_loadmore.setText("收回");
		//				im_loadmore.setImageResource(R.drawable.icon_comment_return);
		//			} else if ( status == 1 ) {
		//				tv_loadmore.setText("加载更多");
		//				im_loadmore.setImageResource(R.drawable.icon_comment_expand);
		//			}  else if ( status == 4 ) {
		//				tv_loadmore.setText("正在加载...");
		//				im_loadmore.setVisibility(View.GONE);
		//				im_loadmore_anim.setVisibility(View.VISIBLE);
		//				AnimationDrawable loadingAnim = (AnimationDrawable) im_loadmore_anim.getDrawable();
		//				moreViewHolder.ll_getmore_frame.setClickable(false);
		//				loadingAnim.start();
		//			}  else if ( status == 5 ) {
		//				tv_loadmore.setText("加载失败，请稍后再试");
		//				tv_loadmore.setTextColor(0xff676767);
		//				im_loadmore.setVisibility(View.GONE);
		//			}  
		//		}
	}
	
	/**
	 * 跳转到图文详情页
	 * 
	 * @param url
	 */
	private void gotoImageTextActivity(String url, String title) {
		Intent intent = new Intent(mContext, ImageTextActivity.class);
		intent.putExtra(DefaultConsts.req_url_s, url);
		intent.putExtra(DefaultConsts.title_s, title);
		mContext.startActivity(intent);
	}
	
	private void gotoImageTextActivity(UserCommentBean bean) {
		Intent intent = new Intent(mContext, ImageTextActivity.class);
		intent.putExtra(DefaultConsts.req_url_s, bean.htmlUrl);
		intent.putExtra(DefaultConsts.title_s, bean.title);
		intent.putExtra(DefaultConsts.COMMENT_TARGET_TYPE, String.valueOf(bean.objType));
		intent.putExtra(DefaultConsts.COMMENT_TARGET_VALUE, String.valueOf(bean.objId));
		mContext.startActivity(intent);
	}
	
	/**
	 * 跳转到查看图集的页面
	 * 
	 * @param position
	 * @param type
	 *            1 为直接进入查看单张图片, 2 为可能要进入到瀑布流页面
	 */
	private void gotoImageViewerActivity(int position, int type) {
		UserCommentBean commentBean = myComments.get(position);
		int imgCounts = commentBean.getImageCollectionNum();
		Intent intent;
		if (imgCounts == 0)
		{
			Toast.makeText(mContext, "图集已下架", Toast.LENGTH_SHORT).show();
			return;
		}
		if (type == 1 || (type == 2 && imgCounts <= RegionContentView.IMAGE_COUNT))
		{
			//直接进入看图片
			intent = new Intent(mContext, AtlasSeeActivity.class);
			intent.putExtra(AtlasSeeActivity.FROM_TYPE, DefaultConsts.TAG_FROM_ENTERPRISE);
			intent.putExtra(AtlasSeeActivity.IMAGE_COUNT, imgCounts);
		}
		else
		{//进入到瀑布流页面
			intent = new Intent(mContext, AtlasActivity.class);
		}
		//设置图集的数据
		intent.putExtra(AtlasActivity.ATLAS_ID, commentBean.getObjId());
		mContext.startActivity(intent);
	}
	
	private class MyCommentViewHolder
	{
		View           ll_object_frame, ll_getmore_frame;
		ImageView im_cover;
		TextView       tv_title, tv_description, tv_get_more;
		MyListView     lv_commentitem;
		ImageView      im_get_more, im_getmore_anim;
	}
	
	public static class CommentItemViewHolder
	{
		CircleImageView im_head;
		TextView        tv_name, tv_time, tv_content;
	}
	
	public static class MoreViewHolder
	{
		View     ll_getmore_frame;
		TextView tv_get_more;
		ImageView im_get_more, im_getmore_anim;
		int       status;                      //1为加载更多， 2 为收回， 3为隐藏， 4为正在加载， 5 为加载失败
	}
	
	public static class CommentItemsAdapter
	        extends
	        BaseAdapter
	{
		private List<CommentBean> commentItemsList;
		private int               parentPosition;
		private Context           myContext;
		
		public CommentItemsAdapter(Context context) {
			super();
			myContext = context;
		}
		
		public void setCommentItemsList(List<CommentBean> itemsList) {
			commentItemsList = itemsList;
		}
		
		public void setParentPosition(int parentPosition) {
			this.parentPosition = parentPosition;
		}
		
		@Override
		public int getCount() {
			if (commentItemsList == null) return 0;
			return commentItemsList.size();
		}
		
		@Override
		public Object getItem(int position) {
			return commentItemsList.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View view, ViewGroup parent) {
			view = LayoutInflater.from(myContext).inflate(R.layout.comment_comment_content_item, null);
			CommentItemViewHolder ItemViewHolder = new CommentItemViewHolder();
			ItemViewHolder.im_head = (CircleImageView) view.findViewById(R.id.im_head);
			ItemViewHolder.tv_name = (TextView) view.findViewById(R.id.tv_name);
			ItemViewHolder.tv_time = (TextView) view.findViewById(R.id.tv_time);
			ItemViewHolder.tv_content = (TextView) view.findViewById(R.id.tv_content);
			view.setTag(parentPosition);
			//设置内容
			CommentBean commentItem = commentItemsList.get(position);
			if (Util.isNotEmpty(commentItem.getCommentUserHeadImage()))
			{
				ImageLoaderUtil.loadWebUrl(commentItem.getCommentUserHeadImage(), ItemViewHolder.im_head);
			}
			ItemViewHolder.tv_name.setText(getNickName(commentItem));
			ItemViewHolder.tv_time.setText(DateUtil.getRegionTime(commentItem.getCreateTime()));
			ItemViewHolder.tv_content.setText(getContent(commentItem));
			return view;
		}
		
		private String getContent(CommentBean bean) {
			if (null != bean) { return Util.isEmpty(bean.replyCommentUserNickName) ? bean.content : String.format("回复%s: %s", bean.replyCommentUserNickName, bean.content); }
			return "";
		}
		
		private String getNickName(CommentBean bean) {
			if (null == bean) return "";
			if (Util.isNotEmpty(bean.commentUserNickName)) return bean.commentUserNickName;
			final String account = bean.commentUserAccount;
			if (Util.isEmpty(account)) return "";
			return Util.formatPhone(account);
		}
	}
	
	private void getMycomments() {
		currentPage = 1;
		currentPublishTime = 0;
		hasMore = true;
		requestComments();
	}
	
	private void getMoreMycomments() {
		currentPage++;
		requestComments();
	}
	
	private void requestComments() {
		QryCommentListByUserAction mycommentsAction = new QryCommentListByUserAction(this, currentPublishTime, PAGESIZE, INIT_COMMENTITEMS_SIZE, new MyCommentActionListener());
		mycommentsAction.start();
	}
	
	/** 获取我的评论的 action listener */
	private class MyCommentActionListener
	        extends
	        BaseAction
	{
		@SuppressWarnings ("unchecked")
		@Override
		public void OK(final Object obj) {
			//刷新UI
			handler.post(new Runnable()
			{
				@Override
				public void run() {
					List<UserCommentBean> comments = (List<UserCommentBean>) obj;
					if (comments == null || comments.size() == 0)
					{
						//取到的内空为空
						hasMore = false;
						handler.post(new Runnable()
						{
							@Override
							public void run() {
								//todo 结束装载更多的动画
								stopLoadOrRefresh();
								if (isInitLoad)
								{//只有当前是初始装载数据时， 才更新ui为无内容
									updateHintUI(1);// 内容为空
								}
							}
						});
						return;
					}
					
					handleHasMoreComments(comments);
					hasMore = true;
					if (currentPage == 1)
					{//刷新
						myComments = comments;
					}
					else
					{//取下一页
						myComments.addAll(comments);
					}
					UserCommentBean lastItem = myComments.get(myComments.size() - 1);
					currentPublishTime = lastItem.getLastCommentTime();
					
					myAdapter.notifyDataSetChanged();
					stopLoadOrRefresh();
					if (isInitLoad)
					{
						isInitLoad = false;
						// 结束装载动画
						updateHintUI(2); //更新成功
					}
				}
			});
		}
		
		@Override
		public void onError() {
			super.onError();
			//通知 外部的调用者， 出错了。
			handler.post(new Runnable()
			{
				@Override
				public void run() {
					//todo 处理UI的不同显示
					stopLoadOrRefresh();
					if (currentPage == 1)
					{//刷新或初始装载数据
						updateHintUI(-1);//失败
					}
					//获取更多评论失败
					//Util.showToast(mContext, "获取评论失败");
				}
			});
		}
	}
	
	private void handleHasMoreComments(List<UserCommentBean> comments) {
		for (UserCommentBean comment : comments)
		{
			List<CommentBean> commentItems = comment.getCommentList();
			if (commentItems != null && commentItems.size() == INIT_COMMENTITEMS_SIZE)
			{
				comment.setHasMoreComments(true);
				commentItems.remove(INIT_COMMENTITEMS_SIZE - 1);//移除最后一条
			}
			else
			{
				comment.setHasMoreComments(false);
			}
		}
		
	}
	
	private void stopLoadOrRefresh() {
		if (currentPage == 1)
		{
			lv_mycomment.stopRefresh();
		}
		else
		{
			lv_mycomment.hideLoadMore();
		}
	}
	
	private void getMoreCommentItems(final String targetType, final String targetValue, final int parentPosition) {
		//执行收回
		UserCommentBean commentBean = myComments.get(parentPosition);
		List<CommentBean> items = commentBean.getCommentList();
		//		if( items != null && items.size() > INIT_COMMENTITEMS_SIZE) {
		//    		CommentItemsAdapter itemsAdapter = commentBean.getItemsAdapter();
		//    		itemsAdapter.setCommentItemsList(items);
		//    		//更新load view 的状态为收回
		//    		updateLoadmoreViewStatus(parentPosition, 2); 
		//    		myAdapter.notifyDataSetChanged();
		//		} else {
		updateLoadmoreViewStatus(parentPosition, 4);//正在加载...
		handler.postDelayed(new Runnable()
		{
			@Override
			public void run() {
				QryCommentMoreByUserAction commentItemsAction = new QryCommentMoreByUserAction(MyCommentActivity.this, targetType, targetValue, 0, 0, new CommentItemsActionListener(parentPosition));
				commentItemsAction.start();
			}
			
		}, 2000);
		//		}
	}
	
	/** 获取我的评论中更多的评论项的 action listener */
	private class CommentItemsActionListener
	        extends
	        BaseAction
	{
		private int parentPosition;
		
		public CommentItemsActionListener(int parentPositioin) {
			this.parentPosition = parentPositioin;
		}
		
		@SuppressWarnings ("unchecked")
		@Override
		public void OK(Object obj) {
			List<CommentBean> commentItems = (List<CommentBean>) obj;
			if (commentItems == null || commentItems.size() < INIT_COMMENTITEMS_SIZE)
			{
				UserCommentBean commentBean = myComments.get(parentPosition);
				commentBean.setCommentList(commentItems);
				final CommentItemsAdapter itemsAdapter = commentBean.getItemsAdapter();
				itemsAdapter.setCommentItemsList(commentItems);
				handler.post(new Runnable()
				{
					@Override
					public void run() {
						//处理UI
						//Util.showToast(mContext, "没有更多评论了");
						updateLoadmoreViewStatus(parentPosition, 3);//没有更多
					}
				});
				return;
			}
			UserCommentBean commentBean = myComments.get(parentPosition);
			commentBean.setCommentList(commentItems);
			final CommentItemsAdapter itemsAdapter = commentBean.getItemsAdapter();
			itemsAdapter.setCommentItemsList(commentItems);
			commentBean.getMoreView().status = 2;
			//刷新UI
			handler.post(new Runnable()
			{
				@Override
				public void run() {
					//更新子list view
					myAdapter.notifyDataSetChanged();
				}
			});
		}
		
		@Override
		public void onError() {
			super.onError();
			//通知 外部的调用者， 出错了。
			handler.post(new Runnable()
			{
				@Override
				public void run() {
					//todo 处理UI的不同显示
					//获取更多评论失败
					//Util.showToast(mContext, "获取更多评论失败");
					updateLoadmoreViewStatus(parentPosition, 5);//加载失败
				}
			});
		}
	}
	
	private void refreshCommentItems(final String targetType, final String targetValue, final int parentPosition) {
		//执行收回
		int itemCount = 0;
		UserCommentBean comment = myComments.get(parentPosition);
		MoreViewHolder moreView = comment.getMoreView();
		if (moreView.status == 1)
		{//加载更多
			itemCount = INIT_COMMENTITEMS_SIZE;
		}
		QryCommentMoreByUserAction commentItemsAction = new QryCommentMoreByUserAction(this, targetType, targetValue, 0, itemCount, new RefreshCommentItemsActionListener(parentPosition));
		commentItemsAction.start();
	}
	
	/** 获取我的评论中更多的评论项的 action listener */
	private class RefreshCommentItemsActionListener
	        extends
	        BaseAction
	{
		private int parentPosition;
		
		public RefreshCommentItemsActionListener(int parentPositioin) {
			this.parentPosition = parentPositioin;
		}
		
		@SuppressWarnings ("unchecked")
		@Override
		public void OK(Object obj) {
			List<CommentBean> commentItems = (List<CommentBean>) obj;
			if (commentItems == null || commentItems.size() < INIT_COMMENTITEMS_SIZE)
			{
				UserCommentBean commentBean = myComments.get(parentPosition);
				commentBean.setCommentList(commentItems);
				final CommentItemsAdapter itemsAdapter = commentBean.getItemsAdapter();
				itemsAdapter.setCommentItemsList(commentItems);
				handler.post(new Runnable()
				{
					@Override
					public void run() {
						//处理UI
						//Util.showToast(mContext, "没有更多评论了");
						updateLoadmoreViewStatus(parentPosition, 3);//没有更多
					}
				});
				return;
			}
			UserCommentBean commentBean = myComments.get(parentPosition);
			MoreViewHolder moreView = commentBean.getMoreView();
			if (moreView.status == 1)
			{//原来的状态为加载更多
				if (commentItems.size() == INIT_COMMENTITEMS_SIZE)
				{
					commentItems.remove(INIT_COMMENTITEMS_SIZE - 1);
					commentBean.setHasMoreComments(true);
				}
			}
			commentBean.setCommentList(commentItems);
			final CommentItemsAdapter itemsAdapter = commentBean.getItemsAdapter();
			itemsAdapter.setCommentItemsList(commentItems);
			//刷新UI
			handler.post(new Runnable()
			{
				@Override
				public void run() {
					//更新子list view
					myAdapter.notifyDataSetChanged();
				}
			});
		}
		
		@Override
		public void onError() {
			super.onError();
			//通知 外部的调用者， 出错了。
			handler.post(new Runnable()
			{
				@Override
				public void run() {
					//todo 处理UI的不同显示
					//获取更多评论失败
					//Util.showToast(mContext, "获取更多评论失败");
					updateLoadmoreViewStatus(parentPosition, 5);//加载失败
				}
			});
		}
	}
	
	private void deleteCommentItem(int parentPosition, String targetType, String targetValue, long commentId) {
		DelCommentAction deleteListener = new DelCommentAction(this, targetType, targetValue, commentId, new DeleteItemActionListener(parentPosition, targetType, targetValue));
		deleteListener.start();
	}
	
	/** 删除我的评论项的 action listener */
	private class DeleteItemActionListener
	        extends
	        BaseAction
	{
		private int    parentPosition;
		private String targetType;
		private String targetValue;
		
		public DeleteItemActionListener(int parentPosition, String targetType, String targetValue) {
			this.parentPosition = parentPosition;
			this.targetType = targetType;
			this.targetValue = targetValue;
		}
		
		@SuppressWarnings ("unchecked")
		@Override
		public void OK(Object obj) {
			Boolean isSuccessful = (Boolean) obj;
			if (isSuccessful)
			{
				//刷新UI
				handler.post(new Runnable()
				{
					@Override
					public void run() {
						//刷新评论
						//getMycomments( );
						refreshCommentItems(targetType, targetValue, parentPosition);
						Util.showToast(mContext, "删除评论成功");
					}
				});
			}
		}
		
		@Override
		public void onError() {
			super.onError();
			//通知 外部的调用者， 出错了。
			handler.post(new Runnable()
			{
				@Override
				public void run() {
					//todo 处理UI的不同显示
					Util.showToast(mContext, "删除评论失败");
				}
			});
		}
	}
	
	//删除评论的对话框
	private class DeleteCommentItemDialog
	        extends
	        Dialog
	{
		//private TextView vContent;
		private TextView                vOK;
		private TextView                vCancel;
		private TextView                tvContent;
		private CheckBox                cbDeleteFile;
		private Context                 myContext;
		private DeleteCommentItemDialog self;
		
		public DeleteCommentItemDialog(Context context) {
			super(context, R.style.common_dialog);
			myContext = context;
			self = this;
			findViews();
			setCanceledOnTouchOutside(true);
			getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			getWindow().setGravity(Gravity.BOTTOM);
		}
		
		private void findViews() {
			View view = LayoutInflater.from(myContext).inflate(R.layout.shelf_delete_dialog, null);
			tvContent = (TextView) view.findViewById(R.id.shelf_delete_content);
			tvContent.setText("确认删除该条评论？");
			vCancel = (TextView) view.findViewById(R.id.shelf_delete_cancel);
			vCancel.setOnClickListener(mOnCancelClickListener);
			vOK = (TextView) view.findViewById(R.id.shelf_delete_ok);
			cbDeleteFile = (CheckBox) view.findViewById(R.id.shelf_delete_local_file);
			cbDeleteFile.setVisibility(View.GONE);
			setContentView(view);
		}
		
		View.OnClickListener mOnCancelClickListener = new View.OnClickListener()
		                                            {
			                                            @Override
			                                            public void onClick(View v) {
				                                            self.dismiss();
			                                            }
		                                            };
		
		public void setOkOnClickListener(View.OnClickListener okListener) {
			vOK.setOnClickListener(okListener);
		}
		
		public void setMesssageContent(String content) {
			tvContent.setText(content);
		}
	}
	
}
